//
//  CDate.cpp
//  lilTerm
//
//  Created by Threese on 2019/7/19.
//  Copyright © 2019 Threese. All rights reserved.
//

#include "CDate.h"

CDate::CDate(){
    m_Year = 0;
    m_Month = 0;
    m_Day = 0;
    m_Hour = 0;
    m_Min = 0;
    m_Sec = 0;
}

CDate::~CDate(){

}

int dayInMonth[] = {31, 28, 31, 30, 31, 30,
    31, 31, 30, 31, 30, 31};


int CDate:: setDate(int year, int month, int day, int hour, int min, int sec){
    if(year < 0 || year > 9999)
        return 1;
    if(month < 0 || month > 12)
        return 2;
    if(day < 0 || day >= dayInMonth[month - 1])
        return 3;
    if(hour < 0 || hour >= 24)
        return 4;
    if(min < 0 || min >= 60)
        return 5;
    if(sec < 0 || sec >= 60)
        return 6;
    
    m_Year = year;
    m_Month = month;
    m_Day = day;
    m_Hour = hour;
    m_Min = min;
    m_Sec = sec;
    
    return 0;
}

unsigned int CDate:: getDay(){
    return m_Day;
}
unsigned int CDate:: getMonth(){
    return m_Month;
}
unsigned int CDate:: getYear(){
    return m_Year;
}
unsigned int CDate:: getHour(){
    return m_Hour;
}
unsigned int CDate:: getMinute(){
    return m_Min;
}
unsigned int CDate:: getSecond(){
    return m_Sec;
}

void CDate:: addTime(int second){
    if(second < 0)
        return ;
    else{
        m_Sec += second;
        
        if(m_Sec >= 60){
            int countS = m_Sec / 60;
            m_Sec %= 60;
            
            m_Min += countS;
        }
        
        if(m_Min >= 60){
            int countM = m_Min / 60;
            m_Min %= 60;
            
            m_Hour += countM;
        }
        
        if(m_Hour >= 24){
            int countH = m_Hour / 24;
            m_Hour %= 24;
            
            addDate(countH);
        }
    }
}

void CDate:: addDate(int day){
    if(day > 28){
        for(int i = 0;i < 2;i++)
            addDate(day / 2);
    }
    
    m_Day += day;
    
    if(m_Day >= dayInMonth[m_Month - 1]){
        m_Day %= dayInMonth[m_Month - 1];
        m_Month++;
    }
    
    if(m_Month >= 12){
        m_Month %= 12;
        m_Year++;
    }
    
    if(m_Year >= 10000){
        m_Year = 0;
    }
}

int CDate::compare(IDate *d1, IDate *d2){
    if(d1->getYear() > d2->getYear())
        return 1;
    else if(d1->getYear() == d2->getYear()){
        if(d1->getMonth() > d2->getMonth())
            return 1;
        else if(d1->getMonth() == d2->getMonth()){
            if(d1->getDay() > d2->getDay())
                return 1;
            else if(d1->getDay() == d2->getDay()){
                if(d1->getHour() > d2->getHour())
                    return 1;
                else if(d1->getHour() == d2->getHour()){
                    if(d1->getMinute() > d2->getMinute())
                        return 1;
                    else if(d1->getMinute() == d2->getMinute() ){
                        if(d1->getSecond() > d2->getSecond())
                            return 1;
                        else if(d1->getSecond() == d2->getSecond())
                            return 0;
                        else
                            return -1;
                    }
                    else
                        return -1;
                }
                else
                    return -1;
            }
            else
                return -1;
        }
        else
            return -1;
    }
    else
        return -1;
}

void CDate::getToday(void){
    time_t nowTime = time(NULL);
    struct tm* t = localtime(&nowTime);
    
    m_Year = t->tm_year + 1900;
    m_Month = t->tm_mon + 1;
    m_Day = t->tm_mday;
    m_Hour = t->tm_hour;
    m_Min = t->tm_min;
    m_Sec = t->tm_sec;
}

unsigned int CDate:: operator-(IDate& date){ // Get period between two Date
    int result = 0;
    dayInMonth[1] = 28;
    
    int year = m_Year - date.getYear();
    
    int mDay1 = 0;
    for(int i = 0;i < this->getMonth();i++)
        mDay1 += dayInMonth[i];
        
    int mDay2 = 0;
    for(int i = 0;i < date.getMonth();i++)
        mDay2 += dayInMonth[i];
    
    int day = m_Day - date.getDay();
    
    result += year * 365 + (mDay1 - mDay2) + day;
    
    if(result >= 0)
        return result;
    else
        return -result;
}
